package com.idanchuang.component.logback.filter;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.idanchuang.component.core.helper.EnvContext;
import com.idanchuang.component.logback.converter.TraceIdPatternConverter;

/**
 *
 * 压测环境过滤
 *
 * @author yjy
 * @date 2020/9/1 10:20
 **/
public class StandardLogFilter extends Filter<ILoggingEvent> {

    private final TraceIdPatternConverter traceIdPatternConverter = new TraceIdPatternConverter();

    @Override
    public FilterReply decide(ILoggingEvent iLoggingEvent) {
        try {
            String tid = traceIdPatternConverter.convert(iLoggingEvent);
            iLoggingEvent.getLoggerContextVO().getPropertyMap().put("tid", tid);
        } catch (Exception e) {
            // ignore
        }
        return EnvContext.isPressureForLog() ? FilterReply.DENY : FilterReply.NEUTRAL;
    }

}
